package com.cade.agriculturemall.view.personalCenter.ordersManage;

import android.text.TextUtils;

import com.avos.avoscloud.AVException;
import com.avos.avoscloud.AVObject;
import com.avos.avoscloud.AVQuery;
import com.avos.avoscloud.AVUser;
import com.avos.avoscloud.DeleteCallback;
import com.avos.avoscloud.FindCallback;
import com.avos.avoscloud.SaveCallback;
import com.cade.agriculturemall.base.BasePresenter;
import com.cade.agriculturemall.model.OrderEntity;
import com.cade.agriculturemall.model.ShoppingCarsEntity;
import com.cade.agriculturemall.model.UserInfoEntity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by hshunxin on 2018/1/29.
 */

public class MyOrdersPresenter extends BasePresenter<MyOrdersContract.IView> implements MyOrdersContract.IPresenter {
    public MyOrdersPresenter(MyOrdersContract.IView view) {
        super(view);
    }

    @Override
    public void onResume() {

    }

    @Override
    public void onCreate() {

    }

    @Override
    public void onDestroy() {

    }

    HashMap<OrderEntity, List<ShoppingCarsEntity>> hashMap = new HashMap<>();

    @Override
    public void downLoadAllEntity(int type) {
        mView.showProgress();
        final AVQuery<OrderEntity> orderAvquery = new AVQuery<>("order");
        AVUser.alwaysUseSubUserClass(UserInfoEntity.class);
        UserInfoEntity user = AVUser.getCurrentUser(UserInfoEntity.class);
        String currentUserId = user.getObjectId();
        orderAvquery.whereEqualTo("user_Id", currentUserId);
        String orderStatus = "";
        switch (type) {
            case 0:
                orderStatus = "";
                break;
            case 1:
                orderStatus = "0001";
                break;
            case 2:
                orderStatus = "0011";
                break;
            case 3:
                orderStatus = "0111";
                break;
            case 4:
                orderStatus = "0000";
                break;
        }
        if (!TextUtils.isEmpty(orderStatus)) {
            orderAvquery.whereEqualTo("order_status", orderStatus);
        }
        orderAvquery.findInBackground(new FindCallback<OrderEntity>() {
            @Override
            public void done(List<OrderEntity> list, AVException e) {
                hashMap.clear();
                if (e == null) {
                    if (list.size() != 0) {
                        for (final OrderEntity orderEntity : list) {
                            AVQuery<ShoppingCarsEntity> shoppingCarsEntityAVQuery = new AVQuery<>("ShoppingCars");
                            shoppingCarsEntityAVQuery.whereEqualTo("orderId", orderEntity);
                            shoppingCarsEntityAVQuery.include("pro");
                            shoppingCarsEntityAVQuery.findInBackground(new FindCallback<ShoppingCarsEntity>() {
                                @Override
                                public void done(List<ShoppingCarsEntity> list, AVException e) {
                                    if (e == null) {
                                        if (list.size() != 0) {
                                            hashMap.put(orderEntity, list);
                                        }
                                        mView.onDownloadEntity(hashMap);
                                    } else {
                                        e.printStackTrace();
                                    }
                                }
                            });
                        }
                    } else {
                        mView.onDownloadEntity(hashMap);
                    }
                } else {
                    e.printStackTrace();
                }
                mView.dismissProgress();
            }
        });
    }

    @Override
    public void orderStatusChange(final OrderEntity orderEntity, String status) {
        mView.showProgress();
        String objectId = orderEntity.getObjectId();
        AVObject avObject = AVObject.createWithoutData("order", objectId);
        avObject.put("order_status", status);
        avObject.saveInBackground(new SaveCallback() {
            @Override
            public void done(AVException e) {
                if (e == null) {
                    mView.showMessage("ok");
                    mView.doViewRefresh(orderEntity);
                } else {
                    e.printStackTrace();
                }
                mView.dismissProgress();
            }
        });
    }


    @Override
    public void goodsDelete(final OrderEntity orderEntity) {
        String objectId = orderEntity.getObjectId();
        mView.showProgress();
        AVQuery<AVObject> deleteOrder = new AVQuery<>("order");
        deleteOrder.whereEqualTo("objectId", objectId);
        deleteOrder.deleteAllInBackground(new DeleteCallback() {
            @Override
            public void done(AVException e) {
                if (e != null) {
                    mView.showDialog();
                    mView.deleteFail();
                } else {
                    mView.deleteSuccess();
                    mView.doViewRefresh(orderEntity);
                }
                mView.dismissProgress();
            }
        });
    }
}
